#include "logger.h"
#include "launch/launch_parser.h"
#include "utils/common_utils.h"
#include "watch_dog/watch_dog.h"

#define VERSION "1.1.1"

static const char *USAGE = R"(Usage: roslaunch [package] <filename>

roslaunch: error: you must specify at least one input file)";

int main(int argn, char **argv)
{
    logger::initLogger("roslaunch");
    if (3 != argn)
    {
        ROS_ERROR("%s", USAGE);
        return 0;
    }

    const std::string &env = CommonUtils::getEnv("LD_LIBRARY_PATH", "");
    ROS_INFO("version:%s, LD_LIBRARY_PATH=%s", VERSION, env.c_str());

    //TODO，这里需要tab补全提示功能
    std::string packageName = argv[1];
    std::string launchName = argv[2];
    ROS_INFO("------- roslaunch, package: %s, launch: %s -------", packageName.c_str(), launchName.c_str());

    std::vector<std::string> launchPathList = CommonUtils::getLaunchPath(packageName, launchName);
    if (launchPathList.empty())
    {
        ROS_ERROR("launch %s is not exist", launchName.c_str());
        return 0;
    }
    if (launchPathList.size() > 1)
    {
        ROS_ERROR("there are the same package launch file exist");
        return 0;
    }

    LaunchParser launchParser;
    std::optional<LaunchNode> launchNode = launchParser.parserLaunchFile(launchPathList.at(0));
    if (!launchNode)
    {
        ROS_ERROR("launch parser fail");
        return 0;
    }

    pWatchDog->startWatchWork(10000);//10s监控一次

    return 0;
}
